package com.ohs.ohsoa.system.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageInfo;
import com.ohs.ohsoa.system.dto.ScheduleDto;
import com.ohs.ohsoa.system.service.ScheduleService;
import com.ohs.ohsoa.system.vo.ScheduleVo;
import com.ohs.ohsoa.utils.auth.UserLoginToken;
import com.ohs.ohsoa.utils.bean.requestBean.PageParam;
import com.ohs.ohsoa.utils.bean.responseBean.ResultBean;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 日程controller
 *
 * @author ccj
 * @since 2021年9月27日
 */
@Api(value = "日程管理", tags = "日程管理")
@RestController
@RequestMapping("/schedule")
public class ScheduleController {

    @Resource
    private ScheduleService scheduleService;


    /**
     * 获取日程列表信息
     *
     * @param pageParam 检索用参数
     * @return 日程列表信息
     */
    @UserLoginToken
    @PostMapping(value = "/select/list")
    @ApiOperation(httpMethod = "POST", value = "获取日程列表信息")
    public ResultBean<PageInfo<ScheduleVo>> scheduleSelectList(@RequestBody PageParam<ScheduleDto> pageParam) {
        List<ScheduleVo> res = scheduleService.scheduleSelectList(pageParam);
        return  ResultBean.success(new PageInfo<>(res));
    }

    /**
     * 根据日程日期查询所有包含这个日期
     *
     * @author ccj
     * @since  2021年9月29日
     * @param  pageParam ’id‘
     * @return 实例对象
     */
    @UserLoginToken
    @PostMapping(value = "/selectDayInclude")
    @ApiOperation(httpMethod = "POST", value = "根据日程日期查询所有包含这个日期")
    public ResultBean<PageInfo<ScheduleVo>> scheduleSelectDayInclude(@RequestBody PageParam<ScheduleDto> pageParam) {
        List<ScheduleVo> res = scheduleService.scheduleSelectDayInclude(pageParam);
        return  ResultBean.success(new PageInfo<>(res));
    }
    /**
     * 根据日程id查询详细信息
     *
     * @author ccj
     * @since  2021年9月29日
     * @param  scheduleDto ’id‘
     * @return 实例对象
     */
    @UserLoginToken
    @PostMapping(value = "/select")
    @ApiOperation(httpMethod = "POST", value = "根据日程id查询详细信息")
    public ResultBean<ScheduleVo> scheduleSelect(@RequestBody ScheduleDto scheduleDto) {
        return ResultBean.success(scheduleService.scheduleSelect(scheduleDto));
    }



    /**
     * 新增
     *
     * @author ccj
     * @since 2021年9月29日
     * @param scheduleDto ’实例对象‘
     * @return 1为成功
     */
    @UserLoginToken
    @PostMapping(value = "/insert")
    @ApiOperation(httpMethod = "POST", value = "新增日程")
    public ResultBean<?> scheduleInsert(@RequestBody  @Valid ScheduleDto scheduleDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBean.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultBean.success(scheduleService.scheduleInsert(scheduleDto));
    }
    /**
     * 根据日期查询日程信息
     *
     * @author ccj
     * @since 2021年9月24日
     * @param scheduleDto 日程信息数据
     * @return 实例对象
     */
    @UserLoginToken
    @PostMapping(value = "/selectAll")
    @ApiOperation(httpMethod = "POST", value = "根据日期查询日程信息")
    public ResultBean<List<ScheduleVo>> scheduleSelectAll(@RequestBody ScheduleDto scheduleDto) {
        List<ScheduleVo> scheduleVos = scheduleService.scheduleSelectAll(scheduleDto);
        return ResultBean.success(scheduleVos);
    }



    /**
     * 删除日程
     *
     * @param scheduleDto 日程数据
     * @return 更新数据
     */
    @UserLoginToken
    @DeleteMapping(value = "/delete")
    @ApiOperation(httpMethod = "DELETE", value = "删除日程")
    public ResultBean<Boolean> scheduleDelete(@RequestBody ScheduleDto scheduleDto) {
        return ResultBean.success(scheduleService.scheduleDelete(scheduleDto));
    }

    /**
     * 修改日程信息
     *
     * @param scheduleDto 日程信息数据
     * @return 更新数据
     */
    @UserLoginToken
    @PutMapping(value = "/update")
    @ApiOperation(httpMethod = "PUT", value = "修改日程信息")
    public ResultBean<Boolean> scheduleUpdate(@RequestBody @Valid ScheduleDto scheduleDto,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResultBean.fail(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return ResultBean.success(scheduleService.scheduleUpdate(scheduleDto));
    }



}
